/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DAO;


import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.StringWriter;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.crypto.NoSuchPaddingException;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.KeySpec;
import javax.crypto.Cipher;
import javax.crypto.CipherOutputStream;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;

/**
 *
 * @author ThanhTran
 */
public class DataManager {
    public int count_database;
    public Database[] Database_Arr;
    public String security_key = "12345678";
    
   public DataManager()
    {
        count_database = 0;
        
    }
    
    
    
    //Dọc dữ liệu lên
    public void ReadDatabase()
    {
        Database termDatabase[];
        Table termTable[];
        Column termColumn[];
        Object termRownArr[];
        try { 
                File outputFile = new File("src/Data/Database.xml");
                File inputFile  = new File("src/Data/Database.encrypt");
                //Encrypt(security_key,outputFile, inputFile);
                Decrypt(security_key,inputFile,outputFile) ;
		File fXmlFile = new File(outputFile.getAbsolutePath());
		DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
		DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
		Document doc = dBuilder.parse(fXmlFile);
		doc.getDocumentElement().normalize();
 
		NodeList nListDatabase = doc.getElementsByTagName("Database");
                count_database = nListDatabase.getLength();
                Database_Arr = new Database[count_database];
                
		for (int i = 0; i < nListDatabase.getLength(); i++) {
                    Element Database = (Element) nListDatabase.item(i);
                    Database_Arr[i] = new Database();
                    Database_Arr[i].Name = Database.getAttribute("DataName");
                    
                    
                    NodeList nListTable = Database.getElementsByTagName("Table");
                    Database_Arr[i].count_table = nListTable.getLength();
                    termTable = new Table[nListTable.getLength()];
                    
                    for(int j = 0; j < nListTable.getLength(); j++)
                    {
                        Element Table = (Element)nListTable.item(j);
                        termTable[j] = new Table();
                        termTable[j].Name = Table.getAttribute("TableName");
                        
                        
                        NodeList nListColumn = Table.getElementsByTagName("Column");
                        termTable[j].count_colum = nListColumn.getLength();
                        termColumn = new Column[nListColumn.getLength()];
                        
                        for(int k = 0; k < nListColumn.getLength(); k++)
                        {
                            Element Column = (Element)nListColumn.item(k);
                            String type = Column.getAttribute("DataType");
                            termColumn[k] = new Column();
                            termColumn[k].Type = Boolean.parseBoolean(type);
                            termColumn[k].Name = Column.getAttribute("ColumnName");
                            termColumn[k].isPrimary = Integer.parseInt(Column.getAttribute("isPrimary"));
                            
                            
                            NodeList nListRown = Column.getElementsByTagName("Rown");
                                
                            termRownArr = new Object[nListRown.getLength()];
                            termColumn[k].count_rown = nListRown.getLength();
                                
                            if(type.equals("true"))
                            {                                
                                for(int l = 0; l < nListRown.getLength(); l++)
                                {
                                    Node Rown = (Node)nListRown.item(l);
                                    int tam = Integer.parseInt(Rown.getTextContent());
                                    termRownArr[l] = tam;
                                }
                            }else
                            {
                                for(int l = 0; l < nListRown.getLength(); l++)
                                {
                                    Node Rown = (Node)nListRown.item(l);
                                    termRownArr[l] = Rown.getTextContent();
                                }
                            }
                            
                            termColumn[k].Rown = termRownArr;
                          }
                          termTable[j].Colum_name = termColumn;
                      }
                    Database_Arr[i].Table_Name = termTable;
		}
                outputFile.delete();
	  } catch (Exception e) {
              JFrame j = new JFrame();
              JOptionPane.showMessageDialog(j, "Can't open file");
		e.printStackTrace();
	  }
    }
    
    public boolean CreateDatabase(String database_Name)
    {
        
        //Kiểm tra tên Database có chưa
        for(int i = 0; i < this.count_database; i++)
        {
            if(this.Database_Arr[i].Name.equals(database_Name))
                return false;
        }
              
        try {
             File outputFile = new File("src/Data/Database.xml");
             File inputFile  = new File("src/Data/Database.encrypt");
             Decrypt(security_key,inputFile,outputFile) ;
	     File fXmlFile = new File(outputFile.getAbsolutePath());
            DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
            Document doc = dBuilder.parse(fXmlFile);
            doc.getDocumentElement().normalize();

            //Dọc node gốc
            Element root = doc.getDocumentElement();

            Element database = doc.createElement("Database");
            root.appendChild(database);

            Attr name = doc.createAttribute("DataName");
            name.setNodeValue(database_Name);
            database.setAttributeNode(name);

            TransformerFactory factory = TransformerFactory.newInstance(); 
            Transformer transformer = factory.newTransformer(); 
            
            transformer.setOutputProperty(OutputKeys.INDENT, "yes"); 
        
            
            StringWriter sw = new StringWriter(); 
            StreamResult result = new StreamResult(sw); 
            DOMSource source = new DOMSource(doc); 
            transformer.transform(source, result); 
            String xmlString = sw.toString(); 
            FileWriter fw=new FileWriter(fXmlFile,false); 
            BufferedWriter bw = new BufferedWriter(fw); 
            bw.write(xmlString); 
            bw.flush(); 
            bw.close(); 
            try {
                Encrypt(security_key,fXmlFile, inputFile);
            } catch (NoSuchPaddingException ex) {
                Logger.getLogger(DataManager.class.getName()).log(Level.SEVERE, null, ex);
            }
            outputFile.delete();
        } catch (TransformerException ex) {
            Logger.getLogger(DataManager.class.getName()).log(Level.SEVERE, null, ex);
        }catch (SAXException ex) {
            Logger.getLogger(DataManager.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(DataManager.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ParserConfigurationException ex) {
            Logger.getLogger(DataManager.class.getName()).log(Level.SEVERE, null, ex);
        }
        ReadDatabase();
        return true;
    }
    
    public boolean DropDatabase(String database_Name)
    {
        //ReadDatabase();
        boolean res = false;
        //ReadDatabase();
        //Kiểm tra tên Database có chưa
        for(int i = 0; i < this.count_database; i++)
        {
            if(this.Database_Arr[i].Name.equals(database_Name))
                res = true;
        }
        
        if(res){
            try {
                //File fXmlFile = new File("src/Data/Database.xml");
                 File outputFile = new File("src/Data/Database.xml");
                File inputFile  = new File("src/Data/Database.encrypt");
                Decrypt(security_key,inputFile,outputFile) ;
		File fXmlFile = new File(outputFile.getAbsolutePath());
                DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
                DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
                Document doc = dBuilder.parse(fXmlFile);
                doc.getDocumentElement().normalize();

                Element root = doc.getDocumentElement();

                NodeList nListDatabase = doc.getElementsByTagName("Database");
                for (int i = 0; i < nListDatabase.getLength(); i++) {
                    Element Database = (Element) nListDatabase.item(i);
                    String dataname = Database.getAttribute("DataName");
                    if(dataname.equals(database_Name))
                    {
                        root.removeChild(Database);
                    }
                }
                TransformerFactory factory = TransformerFactory.newInstance(); 
                Transformer transformer = factory.newTransformer(); 

                transformer.setOutputProperty(OutputKeys.INDENT, "yes"); 
                
                StringWriter sw = new StringWriter(); 
                StreamResult result = new StreamResult(sw); 
                DOMSource source = new DOMSource(doc); 
                transformer.transform(source, result); 
                String xmlString = sw.toString(); 
                FileWriter fw=new FileWriter(fXmlFile,false); 
                BufferedWriter bw = new BufferedWriter(fw); 
                bw.write(xmlString); 
                bw.flush(); 
                bw.close(); 
                  try {
                Encrypt(security_key,fXmlFile, inputFile);
               } catch (NoSuchPaddingException ex) {
                Logger.getLogger(DataManager.class.getName()).log(Level.SEVERE, null, ex);
                }
                outputFile.delete();
            } catch (TransformerException ex) {
                Logger.getLogger(DataManager.class.getName()).log(Level.SEVERE, null, ex);
            }catch (SAXException ex) {
                Logger.getLogger(DataManager.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IOException ex) {
                Logger.getLogger(DataManager.class.getName()).log(Level.SEVERE, null, ex);
            } catch (ParserConfigurationException ex) {
                Logger.getLogger(DataManager.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        ReadDatabase();
        return true;
    }
    
    public boolean Add_Table_To_Database(String database_Name, Table table)
    {
        
        boolean res = true;
        boolean isDatabase = false;
        //ReadDatabase();
        //Kiểm tra tên Database có chưa
        for(int i = 0; i < this.count_database; i++)
        {
            if(this.Database_Arr[i].Name.equals(database_Name)){
                isDatabase = true;
                for(int j = 0; j < this.Database_Arr[i].count_table; j++)
                {
                    if(this.Database_Arr[i].Table_Name[j].Name.equals(table.Name))
                        res = false;
                }
            }
        }
        if(!isDatabase){
            return false;
        }
        if(!res)
            return false;
        
        try {
                //Kiểm tra xem khoá chính có trùng ko
                boolean isExitPrimary[] = new boolean[table.Colum_name[0].count_rown];
                for(int i = 0; i < isExitPrimary.length; i++){
                    isExitPrimary[i] = false;
                }
                
          //  File fXmlFile = new File("src/Data/Database.xml");
                File outputFile = new File("src/Data/Database.xml");
                File inputFile  = new File("src/Data/Database.encrypt");
                Decrypt(security_key,inputFile,outputFile) ;
		File fXmlFile = new File(outputFile.getAbsolutePath());
            DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
            Document doc = dBuilder.parse(fXmlFile);
            doc.getDocumentElement().normalize();

            NodeList nListDatabase = doc.getElementsByTagName("Database");
            for (int i = 0; i < nListDatabase.getLength(); i++) {
                Element Database = (Element) nListDatabase.item(i);
                String dataname = Database.getAttribute("DataName");
                if(dataname.equals(database_Name))
                {
                   Element NewTable = doc.createElement("Table");
                   Database.appendChild(NewTable);

                   Attr TableName = doc.createAttribute("TableName");
                   TableName.setNodeValue(table.Name);
                   NewTable.setAttributeNode(TableName);
                   
                   //Kiem tra khoa chinh co trung lap khong
                   for(int j = 0; j < table.count_colum; j++)
                    {
                        if(table.Colum_name[j].isPrimary == 1)
                        {
                            for(int k = 0; k < table.Colum_name[j].count_rown; k++)
                            {
                                for(int l = 0; l < k; l++)
                                {
                                    if(table.Colum_name[j].Rown[k].toString().equals(table.Colum_name[j].Rown[l].toString())){
                                        isExitPrimary[k] = true;
                                    }
                                }
                            }

                        }
                    }
                   
                   for(int j = 0; j < table.count_colum; j++){
                       Element Column = doc.createElement("Column");
                       NewTable.appendChild(Column);

                       Attr ColumnName = doc.createAttribute("ColumnName");
                       ColumnName.setNodeValue(table.Colum_name[j].Name);
                       Column.setAttributeNodeNS(ColumnName);

                       Attr DataType = doc.createAttribute("DataType");
                       DataType.setNodeValue(new Boolean(table.Colum_name[j].Type).toString());
                       Column.setAttributeNodeNS(DataType);
                       
                       Attr isPrimary = doc.createAttribute("isPrimary");
                       isPrimary.setNodeValue(Integer.toString(table.Colum_name[j].isPrimary));
                       Column.setAttributeNodeNS(isPrimary);
                       
                                              
                       for(int k = 0; k < table.Colum_name[j].count_rown; k++){
                           if(isExitPrimary[k] == false){
                               Element Rown = doc.createElement("Rown");
                               Rown.setTextContent(table.Colum_name[j].Rown[k].toString());
                               Column.appendChild(Rown);
                           }
                       }
                   }
                }
            }

            doc.getDocumentElement().normalize();
            TransformerFactory factory = TransformerFactory.newInstance(); 
            Transformer transformer = factory.newTransformer(); 
            
            transformer.setOutputProperty(OutputKeys.INDENT, "yes"); 
            
            StringWriter sw = new StringWriter(); 
            StreamResult result = new StreamResult(sw); 
            DOMSource source = new DOMSource(doc); 
            transformer.transform(source, result); 
            String xmlString = sw.toString(); 
            FileWriter fw=new FileWriter(fXmlFile,false); 
            BufferedWriter bw = new BufferedWriter(fw); 
            bw.write(xmlString); 
            bw.flush(); 
            bw.close();  
              try {
                Encrypt(security_key,fXmlFile, inputFile);
            } catch (NoSuchPaddingException ex) {
                Logger.getLogger(DataManager.class.getName()).log(Level.SEVERE, null, ex);
            }
            outputFile.delete();
        } catch (TransformerException ex) {
            Logger.getLogger(DataManager.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SAXException ex) {
            Logger.getLogger(DataManager.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(DataManager.class.getName()).log(Level.SEVERE, null, ex);
        }catch (ParserConfigurationException ex) {
            Logger.getLogger(DataManager.class.getName()).log(Level.SEVERE, null, ex);
        }
        ReadDatabase();
        return true;
    }
    
    public String[] GetList_Database()
    {
        ReadDatabase();
        if(this.count_database <= 0){
            return null;
        }
        String [] ListDatabase = new String[this.count_database];
        for(int i = 0; i < this.count_database; i++)
        {
            ListDatabase[i] = this.Database_Arr[i].Name;
        }
        return ListDatabase;
    }
    
    public String[] GetList_Table(String database_Name)
    {
        ReadDatabase();
        boolean result = false;
        String[] ListTable = null;
        for(int i = 0; i < this.count_database; i++)
        {
            if(this.Database_Arr[i].Name.equals(database_Name)){
                result = true;
                if(this.Database_Arr[i].count_table <= 0)
                    return null;
                ListTable = new String[this.Database_Arr[i].count_table];
                for(int j = 0; j < ListTable.length; j++){
                    ListTable[j] = this.Database_Arr[i].Table_Name[j].Name;
                }
            }
                
        }
        if(!result)
            return null;
        return ListTable;
    }
    
    public boolean Insert_Table(String database_Name, Table _table)
    {
        //ReadDatabase();
        boolean result = false;
        boolean  isDatabase = false;
        for(int i = 0; i < this.count_database; i++)
        {
            if(this.Database_Arr[i].Name.equals(database_Name)){
                isDatabase = true;
                for(int j = 0; j < this.Database_Arr[i].count_table; j++)
                {
                    if(this.Database_Arr[i].Table_Name[j].Name.equals(_table.Name))
                    {
                        result = true;
                    }
                }
            }
        }
        
        if(!isDatabase || !result)
            return false;
        
        try {
                boolean isExitPrimary[] = new boolean[_table.Colum_name[0].count_rown];
                for(int i = 0; i < isExitPrimary.length; i++){
                    isExitPrimary[i] = false;
                }
           // File fXmlFile = new File("src/Data/Database.xml");
                File outputFile = new File("src/Data/Database.xml");
                File inputFile  = new File("src/Data/Database.encrypt");
                Decrypt(security_key,inputFile,outputFile) ;
		File fXmlFile = new File(outputFile.getAbsolutePath());
		DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
		DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
		Document doc = dBuilder.parse(fXmlFile);
		doc.getDocumentElement().normalize();
 
		NodeList nListDatabase = doc.getElementsByTagName("Database");
                
		for (int i = 0; i < nListDatabase.getLength(); i++) {
                    Element Database = (Element) nListDatabase.item(i);
                    if(Database.getAttribute("DataName").equals(database_Name)){
                    
                        NodeList nListTable = Database.getElementsByTagName("Table");

                        for(int j = 0; j < nListTable.getLength(); j++)
                        {
                            Element Table = (Element)nListTable.item(j);
                            
                            if(Table.getAttribute("TableName").equals(_table.Name)){
                                NodeList nListColumn = Table.getElementsByTagName("Column");

                                for(int k = 0; k < nListColumn.getLength(); k++)
                                {
                                    Element Column = (Element)nListColumn.item(k);
                                    //String type = Column.getAttribute("DataType");
                                    
                                    //Kiem tra khoa chinh co trung lap khong
                                    if(Column.getAttribute("isPrimary").equals(Integer.toString(1)))
                                    {
                                        NodeList ntestRownList = Column.getElementsByTagName("Rown");
                                        for(int m = 0; m < _table.count_colum; m++)
                                        {
                                            if(Column.getAttribute("ColumnName").equals(_table.Colum_name[m].Name)){
                                                for(int t = 0; t < ntestRownList.getLength(); t++){
                                                    Element testRown = (Element)ntestRownList.item(t);
                                            
                                                    for(int n = 0; n < _table.Colum_name[m].count_rown; n++)
                                                    {
                                                        if(testRown.getTextContent().equals(_table.Colum_name[m].Rown[n].toString())){
                                                            isExitPrimary[t] = true;
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                        
                                    }
                                }
                                
                                for(int k = 0; k < nListColumn.getLength(); k++)
                                {
                                    Element Column = (Element)nListColumn.item(k);
                                    //String type = Column.getAttribute("DataType");
                                    
                                    if(Column.getAttribute("ColumnName").equals(_table.Colum_name[k].Name)){
                                        for(int l = 0; l < _table.Colum_name[k].count_rown; l++){
                                            if(isExitPrimary[l] == false){
                                                Element Rown = doc.createElement("Rown");
                                                Rown.setTextContent(_table.Colum_name[k].Rown[l].toString());
                                                Column.appendChild(Rown);
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
		}

                
            doc.getDocumentElement().normalize();
            TransformerFactory factory = TransformerFactory.newInstance(); 
            Transformer transformer = factory.newTransformer(); 
            
            transformer.setOutputProperty(OutputKeys.INDENT, "yes"); 
            
            StringWriter sw = new StringWriter(); 
            StreamResult res = new StreamResult(sw); 
            DOMSource source = new DOMSource(doc); 
            transformer.transform(source, res); 
            String xmlString = sw.toString(); 
            FileWriter fw=new FileWriter(fXmlFile,false); 
            BufferedWriter bw = new BufferedWriter(fw); 
            bw.write(xmlString); 
            bw.flush(); 
            bw.close();  
              try {
                Encrypt(security_key,fXmlFile, inputFile);
            } catch (NoSuchPaddingException ex) {
                Logger.getLogger(DataManager.class.getName()).log(Level.SEVERE, null, ex);
            }
            outputFile.delete();
        } catch (TransformerException ex) {
            Logger.getLogger(DataManager.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SAXException ex) {
            Logger.getLogger(DataManager.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(DataManager.class.getName()).log(Level.SEVERE, null, ex);
        }catch (ParserConfigurationException ex) {
            Logger.getLogger(DataManager.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        ReadDatabase();
        return result;
    }
    
    public Table[] Select_Table(String database_Name, String[] List_Table_Name, String[] Column_Name, Condition con, boolean Type_Select)
    {
        Table[] termTable = null;
        return termTable;
    }
    
    public boolean Update_Table(String database_Name, String table_Name,Column[] Column_List, Condition[] con)
    {
        boolean result = false;
        boolean  isDatabase = false;
        int indexPrimary = -1;
        boolean isExitColumn = true;
        boolean isCheckColum[] = new boolean[Column_List.length];
        for(int i = 0; i < isCheckColum.length; i++){
            isCheckColum[i] = false;
        }
        
        
        //Kiểm tra nếu khoá chính đã tồn tại thì trả về falses
        for(int j = 0; j < this.count_database; j++)
        {
            if(this.Database_Arr[j].Name.equals(database_Name)){
                isDatabase = true; //Đã tồn tại Database
                
                for(int k = 0; k < this.Database_Arr[j].count_table; k++)
                {
                    if(this.Database_Arr[j].Table_Name[k].Name.equals(table_Name))
                    {
                        result = true; //Đã tồn tại Table
                        
                        for(int l = 0; l < this.Database_Arr[j].Table_Name[k].count_colum; l++){
                            if(this.Database_Arr[j].Table_Name[k].Colum_name[l].isPrimary == 1){
                                for(int i = 0; i < Column_List.length; i++){
                                    if(this.Database_Arr[j].Table_Name[k].Colum_name[l].Name.equals(Column_List[i].Name )){
                                        for(int m = 0; m < this.Database_Arr[j].Table_Name[k].Colum_name[l].count_rown; m++){
                                            if(this.Database_Arr[j].Table_Name[k].Colum_name[l].Rown[m].toString().equals(Column_List[j].Rown[0].toString()))
                                                indexPrimary = m;
                                        }
                                    }
                                }
                            }
                        }
                        
                        //Kiểm tra tất cả các cột truyền vào có tồn tại trong csdl chưa
                        for(int i = 0; i < Column_List.length; i++){
                            for(int l = 0; l < this.Database_Arr[j].Table_Name[k].count_colum; l++){
                                if(Column_List[i].Name.equals(this.Database_Arr[j].Table_Name[k].Colum_name[l].Name)){
                                    if(isCheckColum[i])
                                        return false;
                                    else
                                        isCheckColum[i] = true;
                                }
                            }
                        }
                    }
                }
            }
        }
        
        //Tên cột đưa vào ko tồn tại trong csdl
        for(int i = 0; i < isCheckColum.length; i++){
            if(!isCheckColum[i])
                return false;
        }
        //Tên Database ko tồn tại
        if(!isDatabase || !result)
            return false;
        
//        if(indexPrimary == -1)
//            return false;
        
        try {
                //File fXmlFile = new File("src/Data/Database.xml");
                 File outputFile = new File("src/Data/Database.xml");
                File inputFile  = new File("src/Data/Database.encrypt");
                Decrypt(security_key,inputFile,outputFile) ;
		File fXmlFile = new File(outputFile.getAbsolutePath());
                DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
                DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
                Document doc = dBuilder.parse(fXmlFile);
                doc.getDocumentElement().normalize();

                Element root = doc.getDocumentElement();

                NodeList nListDatabase = doc.getElementsByTagName("Database");
                for (int i = 0; i < nListDatabase.getLength(); i++) {
                    Element Database = (Element) nListDatabase.item(i);
                    String dataname = Database.getAttribute("DataName");
                    if(dataname.equals(database_Name))
                    {
                        NodeList nListTable = Database.getElementsByTagName("Table");
                        for(int j = 0; j < nListTable.getLength(); j++){
                            Element Table = (Element)nListTable.item(j);
                            if(Table.getAttribute("TableName").equals(table_Name)){
                                NodeList nListColum = Table.getElementsByTagName("Column");
                                boolean iscontinue = true;
                                int idrow = 0;  //đánh dấu id dòng thoả mãn điều kiện đầu tiên
                                int maxrow = 0; //kiểm tra có duyệt hết các dòng trong bảng chưa
                                int count_Rown = 0; //đếm số dòng đã duyệt qua
                                
                                //tìm cột đầu tiên thoả điều kiện thứ nhất
                                for(int k = 0; k < nListColum.getLength(); k++){
                                        Element Column = (Element)nListColum.item(k);
                                        if(Column.getAttribute("ColumnName").equals(con[0].left_condition))
                                        {
                                            NodeList nListRown = Column.getElementsByTagName("Rown");
                                            maxrow = nListRown.getLength();
                                            for(idrow = idrow; idrow < nListRown.getLength(); idrow++){
                                                Element Rown = (Element)nListRown.item(idrow);
                                                if(Rown.getTextContent().equals(con[0].right_condition)){
                                                    count_Rown++;
                                                    break;
                                                }
                                                if(idrow >= maxrow - 1)
                                                    iscontinue = false;
                                            }
                                        }
                                    }
                                
                                //Trường hợp update khoá chính nhưng khoá chính đã tồn tại
                                if(indexPrimary != -1 && idrow != indexPrimary)
                                    return false;
                                if(iscontinue){
                                    //Tìm các cột thoả điều kiện còn lại
                                    for(int l = 1; l < con.length; l++){
                                        for(int k = 0; k < nListColum.getLength(); k++){
                                            Element Column = (Element)nListColum.item(k);
                                            if(Column.getAttribute("ColumnName").equals(con[l].left_condition))
                                            {
                                                NodeList nListRown = Column.getElementsByTagName("Rown");
                                                Element Rown = (Element)nListRown.item(idrow);
                                                if(Rown.getTextContent().equals(con[l].right_condition)){
                                                    count_Rown++;
                                                }
                                            }
                                        }
                                    }
                                    if(count_Rown == con.length){
                                        boolean[] ischeck1 = new boolean[nListColum.getLength()];
                                        boolean[] ischeck2 = new boolean[nListColum.getLength()];
                                        for(int l = 0; l < ischeck1.length; l++){
                                            ischeck1[l] = false;
                                            ischeck2[l] = false;
                                        }
                                        for(int l = 0; l < nListColum.getLength(); l++){
                                            Element Column = (Element)nListColum.item(l);
                                            
                                            if(!ischeck1[l]){
                                                for(int k = 0; k < Column_List.length; k++){
                                                    if(Column.getAttribute("ColumnName").equals(Column_List[k].Name) && !ischeck2[k]){
                                                        NodeList nListRown = Column.getElementsByTagName("Rown");
                                                        Element Rown = (Element)nListRown.item(idrow);
                                                        String ValueRown = Column_List[k].Rown[0].toString();
                                                        Rown.setTextContent(ValueRown);
                                                        ischeck1[l] = true;
                                                        ischeck2[k] = true;
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                TransformerFactory factory = TransformerFactory.newInstance(); 
                Transformer transformer = factory.newTransformer(); 

                //transformer.setOutputProperty(OutputKeys.INDENT, "yes"); 
                
                StringWriter sw = new StringWriter(); 
                StreamResult res = new StreamResult(sw); 
                DOMSource source = new DOMSource(doc); 
                transformer.transform(source, res); 
                String xmlString = sw.toString(); 
                FileWriter fw=new FileWriter(fXmlFile,false); 
                BufferedWriter bw = new BufferedWriter(fw); 
                bw.write(xmlString); 
                bw.flush(); 
                bw.close(); 
                  try {
                Encrypt(security_key,fXmlFile, inputFile);
              } catch (NoSuchPaddingException ex) {
                Logger.getLogger(DataManager.class.getName()).log(Level.SEVERE, null, ex);
              }
                outputFile.delete();
            } catch (TransformerException ex) {
                Logger.getLogger(DataManager.class.getName()).log(Level.SEVERE, null, ex);
            }catch (SAXException ex) {
                Logger.getLogger(DataManager.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IOException ex) {
                Logger.getLogger(DataManager.class.getName()).log(Level.SEVERE, null, ex);
            } catch (ParserConfigurationException ex) {
                Logger.getLogger(DataManager.class.getName()).log(Level.SEVERE, null, ex);
            }
        
        ReadDatabase();
        return result;
    }
    
    public boolean Delete_Table(String database_Name, String table_Name)
    {
        ReadDatabase();
        boolean result = false;
        boolean  isDatabase = false;
        for(int i = 0; i < this.count_database; i++)
        {
            if(this.Database_Arr[i].Name.equals(database_Name)){
                isDatabase = true;
                for(int j = 0; j < this.Database_Arr[i].count_table; j++)
                {
                    if(this.Database_Arr[i].Table_Name[j].Name.equals(table_Name))
                    {
                        result = true;
                    }
                }
            }
        }
        
        if(!isDatabase || !result)
            return false;
        
        try {
               // File fXmlFile = new File("src/Data/Database.xml");
                 File outputFile = new File("src/Data/Database.xml");
                File inputFile  = new File("src/Data/Database.encrypt");
                Decrypt(security_key,inputFile,outputFile) ;
		File fXmlFile = new File(outputFile.getAbsolutePath());
                DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
                DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
                Document doc = dBuilder.parse(fXmlFile);
                doc.getDocumentElement().normalize();

                Element root = doc.getDocumentElement();

                NodeList nListDatabase = doc.getElementsByTagName("Database");
                for (int i = 0; i < nListDatabase.getLength(); i++) {
                    Element Database = (Element) nListDatabase.item(i);
                    String dataname = Database.getAttribute("DataName");
                    if(dataname.equals(database_Name))
                    {
                        NodeList nListTable = Database.getElementsByTagName("Table");
                        for(int j = 0; j < nListTable.getLength(); j++){
                            Element Table = (Element)nListTable.item(j);
                            if(Table.getAttribute("TableName").equals(table_Name)){
                                Database.removeChild(Table);
                            }
                        }
                    }
                }
                TransformerFactory factory = TransformerFactory.newInstance(); 
                Transformer transformer = factory.newTransformer(); 

                transformer.setOutputProperty(OutputKeys.INDENT, "yes"); 
                
                StringWriter sw = new StringWriter(); 
                StreamResult res = new StreamResult(sw); 
                DOMSource source = new DOMSource(doc); 
                transformer.transform(source, res); 
                String xmlString = sw.toString(); 
                FileWriter fw=new FileWriter(fXmlFile,false); 
                BufferedWriter bw = new BufferedWriter(fw); 
                bw.write(xmlString); 
                bw.flush(); 
                bw.close(); 
                  try {
                Encrypt(security_key,fXmlFile, inputFile);
            } catch (NoSuchPaddingException ex) {
                Logger.getLogger(DataManager.class.getName()).log(Level.SEVERE, null, ex);
            }
                outputFile.delete();
            } catch (TransformerException ex) {
                Logger.getLogger(DataManager.class.getName()).log(Level.SEVERE, null, ex);
            }catch (SAXException ex) {
                Logger.getLogger(DataManager.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IOException ex) {
                Logger.getLogger(DataManager.class.getName()).log(Level.SEVERE, null, ex);
            } catch (ParserConfigurationException ex) {
                Logger.getLogger(DataManager.class.getName()).log(Level.SEVERE, null, ex);
            }
        
        ReadDatabase();
        return result;
    }
    
    public boolean Delete_Table(String database_Name, String table_Name, Condition[] con)
    {
        ReadDatabase();
        boolean result = false;
        boolean  isDatabase = false;
        for(int i = 0; i < this.count_database; i++)
        {
            if(this.Database_Arr[i].Name.equals(database_Name)){
                isDatabase = true;
                for(int j = 0; j < this.Database_Arr[i].count_table; j++)
                {
                    if(this.Database_Arr[i].Table_Name[j].Name.equals(table_Name))
                    {
                        result = true;
                    }
                }
            }
        }
        
        if(!isDatabase || !result)
            return false;
        
        try {
               // File fXmlFile = new File("src/Data/Database.xml");
                 File outputFile = new File("src/Data/Database.xml");
                File inputFile  = new File("src/Data/Database.encrypt");
                Decrypt(security_key,inputFile,outputFile) ;
		File fXmlFile = new File(outputFile.getAbsolutePath());
                DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
                DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
                Document doc = dBuilder.parse(fXmlFile);
                doc.getDocumentElement().normalize();

                Element root = doc.getDocumentElement();

                NodeList nListDatabase = doc.getElementsByTagName("Database");
                for (int i = 0; i < nListDatabase.getLength(); i++) {
                    Element Database = (Element) nListDatabase.item(i);
                    String dataname = Database.getAttribute("DataName");
                    if(dataname.equals(database_Name))
                    {
                        NodeList nListTable = Database.getElementsByTagName("Table");
                        for(int j = 0; j < nListTable.getLength(); j++){
                            Element Table = (Element)nListTable.item(j);
                            if(Table.getAttribute("TableName").equals(table_Name)){
                                NodeList nListColum = Table.getElementsByTagName("Column");
                                boolean iscontinue = true;
                                int idrow = 0;  //đánh dấu id dòng thoả mãn điều kiện đầu tiên
                                int maxrow = 0; //kiểm tra có duyệt hết các dòng trong bảng chưa
                                int count_Rown = 0; //đếm số dòng đã duyệt qua
                                while(iscontinue) //đảm bảo duyệt hết bảng
                                {
                                    count_Rown = 0;
                                    //Tìm cột đầu tiên thoả điều kiện thứ nhất
                                    for(int k = 0; k < nListColum.getLength(); k++){
                                            Element Column = (Element)nListColum.item(k);
                                            if(Column.getAttribute("ColumnName").equals(con[0].left_condition))
                                            {
                                                NodeList nListRown = Column.getElementsByTagName("Rown");
                                                maxrow = nListRown.getLength();
                                                for(idrow = idrow; idrow < nListRown.getLength(); idrow++){
                                                    Element Rown = (Element)nListRown.item(idrow);
                                                    if(Rown.getTextContent().equals(con[0].right_condition)){
                                                        count_Rown++;
                                                        break;
                                                    }
                                                    if(idrow >= maxrow - 1)
                                                        iscontinue = false;
                                                }
                                            }
                                        }
                                    if(iscontinue){
                                        //Tìm các cột thoả các điều kiện còn lại
                                        for(int l = 1; l < con.length; l++){
                                            for(int k = 0; k < nListColum.getLength(); k++){
                                                Element Column = (Element)nListColum.item(k);
                                                if(Column.getAttribute("ColumnName").equals(con[l].left_condition))
                                                {
                                                    NodeList nListRown = Column.getElementsByTagName("Rown");
                                                    Element Rown = (Element)nListRown.item(idrow);
                                                    if(Rown.getTextContent().equals(con[l].right_condition)){
                                                        count_Rown++;
                                                    }
                                                }
                                            }
                                        }
                                        //Xoá dòng tương ứng nếu tất cả điều kiện đều thoả
                                        if(count_Rown == con.length){
                                            for(int l = 0; l < nListColum.getLength(); l++){
                                                Element Column = (Element)nListColum.item(l);

                                                NodeList nListRown = Column.getElementsByTagName("Rown");
                                                Element Rown = (Element)nListRown.item(idrow);

                                                Column.removeChild(Rown);
                                            }
                                            idrow--;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                TransformerFactory factory = TransformerFactory.newInstance(); 
                Transformer transformer = factory.newTransformer(); 

                //transformer.setOutputProperty(OutputKeys.INDENT, "yes"); 
                
                StringWriter sw = new StringWriter(); 
                StreamResult res = new StreamResult(sw); 
                DOMSource source = new DOMSource(doc); 
                transformer.transform(source, res); 
                String xmlString = sw.toString(); 
                FileWriter fw=new FileWriter(fXmlFile,false); 
                BufferedWriter bw = new BufferedWriter(fw); 
                bw.write(xmlString); 
                bw.flush(); 
                bw.close();
                  try {
                Encrypt(security_key,fXmlFile, inputFile);
            } catch (NoSuchPaddingException ex) {
                Logger.getLogger(DataManager.class.getName()).log(Level.SEVERE, null, ex);
            }
                outputFile.delete();
            } catch (TransformerException ex) {
                Logger.getLogger(DataManager.class.getName()).log(Level.SEVERE, null, ex);
            }catch (SAXException ex) {
                Logger.getLogger(DataManager.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IOException ex) {
                Logger.getLogger(DataManager.class.getName()).log(Level.SEVERE, null, ex);
            } catch (ParserConfigurationException ex) {
                Logger.getLogger(DataManager.class.getName()).log(Level.SEVERE, null, ex);
            }
        
        return result;
    }
    
    public boolean Add_Column_To_Table(String database_Name, String table_Name, Column _column)
    {
        boolean result = false;
        boolean  isDatabase = false;
        boolean isColumn = true;
        
        ReadDatabase();
        for(int i = 0; i < this.count_database; i++)
        {
            if(this.Database_Arr[i].Name.equals(database_Name)){
                isDatabase = true;
                for(int j = 0; j < this.Database_Arr[i].count_table; j++)
                {
                    if(this.Database_Arr[i].Table_Name[j].Name.equals(table_Name))
                    {
                        result = true;
                        for(int k = 0; k < this.Database_Arr[i].Table_Name[j].count_colum; k++){
                            if(this.Database_Arr[i].Table_Name[j].Colum_name[k].Name.equals(_column.Name))
                                isColumn = false;
                        }
                    }
                }
            }
        }
        
        if(!isDatabase || !result || !isColumn)
            return false;
        
        try {
            
           ///  File fXmlFile = new File("src/Data/Database.xml");
                File outputFile = new File("src/Data/Database.xml");
                File inputFile  = new File("src/Data/Database.encrypt");
                Decrypt(security_key,inputFile,outputFile) ;
		File fXmlFile = new File(outputFile.getAbsolutePath());
		DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
		DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
		Document doc = dBuilder.parse(fXmlFile);
		doc.getDocumentElement().normalize();
 
		NodeList nListDatabase = doc.getElementsByTagName("Database");
                
		for (int i = 0; i < nListDatabase.getLength(); i++) {
                    Element Database = (Element) nListDatabase.item(i);
                    if(Database.getAttribute("DataName").equals(database_Name)){
                    
                        NodeList nListTable = Database.getElementsByTagName("Table");

                        for(int j = 0; j < nListTable.getLength(); j++)
                        {
                            Element Table = (Element)nListTable.item(j);
                            
                            if(Table.getAttribute("TableName").equals(table_Name)){
                                Element NewColumn = doc.createElement("Column");
                                Table.appendChild(NewColumn);

                                Attr ColumnName = doc.createAttribute("ColumnName");
                                ColumnName.setNodeValue(_column.Name);
                                NewColumn.setAttributeNodeNS(ColumnName);

                                Attr DataType = doc.createAttribute("DataType");
                                DataType.setNodeValue(new Boolean(_column.Type).toString());
                                NewColumn.setAttributeNodeNS(DataType);
                                
                                Attr isPrimary = doc.createAttribute("isPrimary");
                                isPrimary.setNodeValue(Integer.toString(_column.isPrimary));
                                NewColumn.setAttributeNodeNS(isPrimary);

                                
                                if(_column.isPrimary == 1){
                                    for(int k = 0; k < _column.Rown.length; k++){
                                        boolean isExit = false;
                                        NodeList nListRown = NewColumn.getElementsByTagName("Rown");

                                        for(int l = 0; l < nListRown.getLength(); l++){
                                            Element testRown = (Element)nListRown.item(l);
                                            if(testRown.getTextContent().equals(_column.Rown[k].toString())){
                                                isExit = true;
                                            }
                                        }
                                        if(!isExit){
                                            Element Rown = doc.createElement("Rown");
                                            Rown.setTextContent(_column.Rown[k].toString());
                                            NewColumn.appendChild(Rown);
                                        }
                                    }
                                }
                                if(_column.isPrimary == 0){
                                    for(int k = 0; k < _column.Rown.length; k++){
                                        Element Rown = doc.createElement("Rown");
                                        Rown.setTextContent(_column.Rown[k].toString());
                                        NewColumn.appendChild(Rown);
                                    }
                                }
                            }
                        }
                    }
		}

                
            doc.getDocumentElement().normalize();
            TransformerFactory factory = TransformerFactory.newInstance(); 
            Transformer transformer = factory.newTransformer(); 
            
            transformer.setOutputProperty(OutputKeys.INDENT, "yes"); 
            
            StringWriter sw = new StringWriter(); 
            StreamResult res = new StreamResult(sw); 
            DOMSource source = new DOMSource(doc); 
            transformer.transform(source, res); 
            String xmlString = sw.toString(); 
            FileWriter fw=new FileWriter(fXmlFile,false); 
            BufferedWriter bw = new BufferedWriter(fw); 
            bw.write(xmlString); 
            bw.flush(); 
            bw.close(); 
              try {
                Encrypt(security_key,fXmlFile, inputFile);
            } catch (NoSuchPaddingException ex) {
                Logger.getLogger(DataManager.class.getName()).log(Level.SEVERE, null, ex);
            }
            outputFile.delete();
        } catch (TransformerException ex) {
            Logger.getLogger(DataManager.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SAXException ex) {
            Logger.getLogger(DataManager.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(DataManager.class.getName()).log(Level.SEVERE, null, ex);
        }catch (ParserConfigurationException ex) {
            Logger.getLogger(DataManager.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        return result;
    }
    
    public boolean Delete_Column_From_Table(String database_Name, String table_Name, String column_Name)
    {
        ReadDatabase();
        boolean result = false;
        boolean  isDatabase = false;
        boolean isColumn = false;
        for(int i = 0; i < this.count_database; i++)
        {
            if(this.Database_Arr[i].Name.equals(database_Name)){
                isDatabase = true;
                for(int j = 0; j < this.Database_Arr[i].count_table; j++)
                {
                    if(this.Database_Arr[i].Table_Name[j].Name.equals(table_Name))
                    {
                        result = true;
                        for(int k = 0; k < this.Database_Arr.length; k++){
                            if(this.Database_Arr[i].Table_Name[j].Colum_name[k].Name.equals(column_Name) && this.Database_Arr[i].Table_Name[j].Colum_name[k].isPrimary == 0)
                                isColumn = true;
                        }
                    }
                }
            }
        }
        
        if(!isDatabase || !result || !isColumn)
            return false;
        
        try {
               //File fXmlFile = new File("src/Data/Database.xml");
                File outputFile = new File("src/Data/Database.xml");
                File inputFile  = new File("src/Data/Database.encrypt");
                Decrypt(security_key,inputFile,outputFile) ;
		File fXmlFile = new File(outputFile.getAbsolutePath());
		DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
		DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
		Document doc = dBuilder.parse(fXmlFile);
		doc.getDocumentElement().normalize();
 
		NodeList nListDatabase = doc.getElementsByTagName("Database");
                
		for (int i = 0; i < nListDatabase.getLength(); i++) {
                    Element Database = (Element) nListDatabase.item(i);
                    if(Database.getAttribute("DataName").equals(database_Name)){
                    
                        NodeList nListTable = Database.getElementsByTagName("Table");

                        for(int j = 0; j < nListTable.getLength(); j++)
                        {
                            Element Table = (Element)nListTable.item(j);
                            
                            if(Table.getAttribute("TableName").equals(table_Name)){
                                NodeList nListColumn = Table.getElementsByTagName("Column");
                                
                                for(int k = 0; k < nListColumn.getLength(); k++){
                                    Element Column = (Element)nListColumn.item(k);
                                    
                                    if(Column.getAttribute("ColumnName").equals(column_Name)){
                                        Table.removeChild(Column);
                                    }
                                }
                            }
                        }
                    }
		}

                
            doc.getDocumentElement().normalize();
            TransformerFactory factory = TransformerFactory.newInstance(); 
            Transformer transformer = factory.newTransformer(); 
            
            transformer.setOutputProperty(OutputKeys.INDENT, "yes"); 
            
            StringWriter sw = new StringWriter(); 
            StreamResult res = new StreamResult(sw); 
            DOMSource source = new DOMSource(doc); 
            transformer.transform(source, res); 
            String xmlString = sw.toString(); 
            FileWriter fw=new FileWriter(fXmlFile,false); 
            BufferedWriter bw = new BufferedWriter(fw); 
            bw.write(xmlString); 
            bw.flush(); 
            bw.close();
               try {
                Encrypt(security_key,fXmlFile, inputFile);
            } catch (NoSuchPaddingException ex) {
                Logger.getLogger(DataManager.class.getName()).log(Level.SEVERE, null, ex);
            }
            outputFile.delete();
        } catch (TransformerException ex) {
            Logger.getLogger(DataManager.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SAXException ex) {
            Logger.getLogger(DataManager.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(DataManager.class.getName()).log(Level.SEVERE, null, ex);
        }catch (ParserConfigurationException ex) {
            Logger.getLogger(DataManager.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        return result;
    }
    
    public Table Get_Data_Table(String database_Name, String table_Name){
        ReadDatabase();
        Table reTable = new Table();
        boolean result = false;
        boolean  isDatabase = false;
        for(int i = 0; i < this.count_database; i++)
        {
            if(this.Database_Arr[i].Name.equals(database_Name)){
                isDatabase = true;
                for(int j = 0; j < this.Database_Arr[i].count_table; j++)
                {
                    if(this.Database_Arr[i].Table_Name[j].Name.equals(table_Name))
                    {
                        result = true;
                        reTable = this.Database_Arr[i].Table_Name[j];
                    }
                }
            }
        }
        
        if(!isDatabase || !result)
            return null;
        return reTable;
    }
    
    public String[] Get_List_Column(String database_Name, String table_Name)
    {
        ReadDatabase();
        String[] ListColumn = null;
        boolean result = false;
        boolean  isDatabase = false;
        for(int i = 0; i < this.count_database; i++)
        {
            if(this.Database_Arr[i].Name.equals(database_Name)){
                isDatabase = true;
                for(int j = 0; j < this.Database_Arr[i].count_table; j++)
                {
                    if(this.Database_Arr[i].Table_Name[j].Name.equals(table_Name))
                    {
                        result = true;
                    }
                }
            }
        }
        
        if(!isDatabase || !result)
            return null;
        for(int i = 0; i < this.count_database; i++){
            if(this.Database_Arr[i].Name.equals(database_Name)){
                for(int j = 0; j < this.Database_Arr[i].count_table; j++){
                    if(this.Database_Arr[i].Table_Name[j].Name.equals(table_Name)){
                        ListColumn = new String[this.Database_Arr[i].Table_Name[j].count_colum];
                        for(int k = 0; k < this.Database_Arr[i].Table_Name[j].count_colum; k++){
                            ListColumn[k] = this.Database_Arr[i].Table_Name[j].Colum_name[k].Name;
                        }
                    }
                }
            }
        }
        return ListColumn;
    }
    
    public void Encrypt(String keyValue, File inputFile, File outputFile) throws NoSuchPaddingException
	{
		//KeyValue phai co do dai la 8 ki tu (tuong ung voi 8 byte), boi vi DES doi hoi key phai co do dai 64 bit.
	    byte[] keyByteArray = keyValue.getBytes();
	    
	    //Tao secret key
	    try {
			KeySpec ks = new DESKeySpec(keyByteArray);
			SecretKeyFactory skf = SecretKeyFactory.getInstance("DES"); 
			SecretKey secretKey = skf.generateSecret(ks);

	    // Tao doi tuong cipher
	    Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");

	 
	    // Khoi tao cac thong so cho cipher: mode (ma hoa hay giai ma), Secret key
	      cipher.init(Cipher.ENCRYPT_MODE, secretKey); 

	     //Qua trinh ma hoa 
	    FileInputStream in = new FileInputStream(inputFile);
			 FileOutputStream fileOut = new FileOutputStream(outputFile); 
			    CipherOutputStream cout = new CipherOutputStream(fileOut, cipher); 
			    byte[] buffer = new byte[1024]; 
			    int length; 
			    try {
					while ((length = in.read(buffer)) != -1) 
					  cout.write(buffer, 0, length);
				    in.close(); 
					 
				    cout.close(); 
				    
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} 
			 
			     

		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
	 catch (InvalidKeyException e1) {
		// TODO Auto-generated catch block
		e1.printStackTrace();
	}
	    catch (NoSuchAlgorithmException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (NoSuchPaddingException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} 
	    catch (InvalidKeySpecException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
	 
	}
	
	public void Decrypt(String keyValue, File inputFile, File outputFile)
	{
		//KeyValue phai co do dai la 8 ki tu (tuong ung voi 8 byte), boi vi DES doi hoi key phai co do dai 64 bit.
		byte[] keyByteArray = keyValue.getBytes();
	    
	    //Tao secret key
	    try {
			KeySpec ks = new DESKeySpec(keyByteArray);
			SecretKeyFactory skf = SecretKeyFactory.getInstance("DES"); 
			SecretKey secretKey = skf.generateSecret(ks);

	    // Tao doi tuong cipher
	    Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");

	 
	    // Khoi tao cac thong so cho cipher: mode (ma hoa hay giai ma), Secret key
	      cipher.init(Cipher.DECRYPT_MODE, secretKey); 

	     //Qua trinh ma hoa 
			FileInputStream in = new FileInputStream(inputFile);
			 FileOutputStream fileOut = new FileOutputStream(outputFile); 
			    CipherOutputStream cout = new CipherOutputStream(fileOut, cipher); 
			    byte[] buffer = new byte[1024]; 
			    int length; 
			    try {
					while ((length = in.read(buffer)) != -1) 
					  cout.write(buffer, 0, length);
				    in.close(); 
					 
				    cout.close(); 
				    
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} 
			 
			     

		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
	 catch (InvalidKeyException e1) {
		// TODO Auto-generated catch block
		e1.printStackTrace();
	}
	    catch (NoSuchAlgorithmException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (NoSuchPaddingException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} 
	    catch (InvalidKeySpecException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
	 
	}
}
